// pages/appointment/appointment.js
const util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*  */
    showNoDataIcon: false,
    userInfo_sj: {},
    /*  */
    /* 客户列表请求参数 */
    appListArgs: {
      page: 1,
      pageSize: 5
    },
    /* 客户列表数据 */
    appData: [],
    datalength: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  getAppointment: function (isRefresh) {
    this.setData({
      showNoDataIcon: false
    })
    if (isRefresh) {
      let existData = this.data.appData
      existData.splice(0, existData.length)
      this.setData({
        appData: existData,
        'appListArgs.page': '1'
      })
    }
    wx.showLoading({
      title: '获取预约',
      mask: true
    })
    var that = this;
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.Url + '/wx/getStoreAppointment',
        method: 'POST',
        header: this.data.userInfo_sj.header,
        data: util.json2Form(this.data.appListArgs),   //
        success: res => {
          console.log(res)
          //token失效
          if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
          if (res.data.code == '1') {
            /* 无数据 */
            if (!res.data.body.data.length) {
              if(!this.data.appData.length){
                this.setData({
                  showNoDataIcon: true
                })
              }
              return resolve()
            }
            const existData = this.data.appData
            res.data.body.data.forEach(item => {
              item.arriveTime = item.arriveTime ? app.globalData.dateFormat2(item.arriveTime) : '-'
              item.isShowDisable = item.status != '0'
              if (item.status == '0') {
                item.statusDesc = '同意'
              } else if (item.status == '1') {
                item.statusDesc = '已同意'
              } else {
                item.statusDesc = '已拒绝'
              }
              existData.push(item)
            })
            let pageNum = this.data.appListArgs.page * 1
            pageNum += 1
            this.setData({
              'appListArgs.page': pageNum + '',
              appData: existData
            })
            resolve()
          } else {
            wx.showToast({
              icon: 'none',
              title: '' + res.data.msg
            })
            if(!this.data.appData.length){
              this.setData({
                showNoDataIcon: true
              })
            }
            reject()
          }
        },
        fail: err => {
          console.log(err)
          wx.showToast({
            icon: 'none',
            title: '获取数据失败'
          })
          if(!this.data.appData.length){
            this.setData({
              showNoDataIcon: true
            })
          }
          reject()
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取用户信息
    this.setData({
      userInfo_sj: app.globalData.get_userInfo()
    })
    //
    this.getAppointment(true)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getAppointment(true)
  },
  callPhone: function (e) {
    let { index } = e.currentTarget.dataset
    app.globalData.setNeedAnimation.call(this, 'appData', this.data.appData, index).then(() => {
      wx.makePhoneCall({
        phoneNumber: e.currentTarget.dataset.mobile
      })
    })
  },
  sencondConfirm: function (e) {
    if (e.currentTarget.dataset.status == '0') {
      var content = '是否' + (e.currentTarget.dataset.confirm == 'true' ? '同意' : '拒绝') + '这次预约？'
      wx.showModal({
        cancelColor: '#666666',
        confirmColor: '#cc3300',
        content: content,
        success: res => {
          console.log(res)
          if (res.confirm) {
            this.updateStatus({
              id: e.currentTarget.dataset.id,
              status: e.currentTarget.dataset.confirm == 'true' ? '1' : '2'
            })
          }
        }
      })
    }
  },
  updateStatus: function (s) {

    wx.request({
      url: app.globalData.Url + '/wx/updateStoreAppointment',
      method: 'POST',
      header: this.data.userInfo_sj.header,
      data: util.json2Form({
        id: s.id,
        status: s.status
      }),   //
      fail: err => {
        console.log(err)
        wx.showToast({
          icon: 'none',
          title: '获取数据失败'
        })
      },
      success: res => {
        //token失效
        if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
        if (res.data.code == '1') {
          this.getAppointment(true)
        }
        wx.showToast({
          icon: 'none',
          title: res.data.msg
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getAppointment()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})